/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.template.plain;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class PlainElement implements PlainNode {

    private String tag;
    private LinkedHashMap<String, String> attributes = new LinkedHashMap<String, String>();
    private List<PlainNode> content = new ArrayList<PlainNode>();

    private PlainElement parent;

    private int location;

    public PlainElement(PlainElement parent, String tag, int location) {
        this.parent = parent;
        this.tag = tag;
        this.location = location;
    }

    public PlainElement getParent() {
        return parent;
    }

    public String getTag() {
        return tag;
    }

    public LinkedHashMap<String, String> getAttributes() {
        return attributes;
    }

    public List<PlainNode> getContent() {
        return content;
    }

    public int getLocation() {
        return location;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (tag != null) {
            sb.append('<').append(tag);
            for (Map.Entry<String, String> entry : attributes.entrySet()) {
                sb.append(' ').append(entry.getKey()).append('=').append('"').append(entry.getValue()).append('"');
            }
            sb.append('>');
        }
        for (Object o : content) {
            sb.append(o);
        }
        if (tag != null) {
            sb.append("</").append(tag).append('>');
        }
        return sb.toString();
    }

    public void setTag(String tag) {
        this.tag = tag;
    }
}
